package nels4561;

import java.util.ArrayList;
import spacewar2.objects.Asteroid;
import spacewar2.objects.SpacewarObject;
import spacewar2.utilities.Position;

/**
 * The base of our A* search. A simple square of the map 
 * holding various information about its contents
 * 
 * @author Michael & Thomas
 */
public class GridCell 
{
	
	/**
	 * The Height of the GridCell
	 */
	public static final int HEIGHT = 15;
	/**
	 * The Width of the GridCell
	 */
	public static final int WIDTH = 15;
	/**
	 * The Diagonal Length of the GridCell
	 */
	public static final double CELLDIAGONAL = (HEIGHT)*(Math.sqrt(2)/2);
	/**
	 * The row of the GridCell within the grid
	 */
	public int _row;
	/**
	 * The column of the GridCell within the grid
	 */
	public int _column;
	/**
	 * The Position of the GridCell
	 */
	public Position _location;
	/**
	 * Whether or not the GridCell contains an Asteroid
	 */
	public boolean containsAsteroid;
	/**
	 * Whether or not the GridCell contains a mineable Asteroid
	 */
	public boolean containsMineableAsteroid;
	/**
	 * Whether or not we should avoid this GridCell while navigating
	 */
	public boolean avoid;
	/**
	 * The Asteroid located in this GridCell
	 */
	public Asteroid Asteroid;
	/**
	 * The general SpacewarObject located in this GridCell
	 */
	public SpacewarObject object;
	/**
	 * The heuristic cost of this GridCell
	 */
	public double h;
	
	
	/**
	 * The GridCell constructor
	 * 
	 * @param row
	 * @param column
	 */
	public GridCell(int row, int column)
	{
		_row = row;
		_column = column;
		
		int y = (row * HEIGHT) + (HEIGHT/2);
		int x = (column * WIDTH) + (WIDTH/2);
		_location = new Position(x, y);
		containsAsteroid = false;
		containsMineableAsteroid = false;
		Asteroid = null;
		object = null;
		avoid = false;
		
		h = Double.MAX_VALUE;
	}
	
	/**
	 * Get the GridCell's adjacent cells within the indicated radius.
	 * 
	 * @param myGrid
	 * @param radius
	 * @return ArrayList<GridCell>
	 */
	public ArrayList<GridCell> getNeighborsWithinRadius(Grid myGrid, int radius)
	{
		ArrayList<GridCell> neighbors = new ArrayList<GridCell>();
		
		for (int i = _row - radius; i < _row + radius + 1; i++)
		{
			for (int j = _column - radius; j < _column + radius + 1; j++)
			{
				int row = i;
				int col = j;
				
				row = (row + myGrid.rows) % myGrid.rows;
				col = (col + myGrid.columns) % myGrid.columns;
				
				neighbors.add(myGrid.grid[row][col]);
			}
		}
		neighbors.remove(this);
		return neighbors;
	}

}
